let app = getApp();
Page({
  ...app.globalData.function,
  data: {
    ...app.globalData.config,
    user_info:{},
    isShow:true,
    Index:[],
    goodsList:[],
    pageNo:1,
    swiperIndex:0,
    winHeight:1200,
    value:5,
    search:"",
    winHeight: "",// 窗口高度
  },


  /* 背景随轮播图而改变 */
  getSwiperIndex(e) {
    // console.log(e);
    let currentIndex = e.detail.current;
    this.setData({
        swiperIndex:currentIndex
    })
},



    onLoad(){
   

     let isShow= this.getStorage('isShow')
     if(!isShow){
        this.setData({
          isShow:true
        })
     }else{
      this.setData({
        isShow:false
      })
     }

      // 计算drawer高度，占满window
    let that = this;
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        let clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        let calc = (clientHeight - 50 - 54) * rpxR - 10;
        that.setData({
          windowHeight:  res.windowHeight,
          windowWidth:  res.windowWidth,
          winHeight: calc
        });
      }
    });
    },


    search(e){
      let search=e.detail.value
      this.setData({
        search,
        pageNo:1,
        goodsList:[]
      })
      this._requestPageList(true);
    },

    onShow(){
      var user_info = this.getStorage('user_info');
      if(user_info){
        this.setData({
          user_info:user_info
        })
      } 
      this.setData({
        goodsList:[]
      })
      this._requestPageList()
    },

    order_pay(events){
      let ordertype=events.currentTarget.dataset.type
      getApp().order_type=ordertype
      // console.log(ordertype)
      wx.switchTab({
        url: '/pages/classify/index',
      })
  },
    //拒接
    rejectInfo(){
      this.setData({
        isShow:false 
      })
    },
    //同意
    getUserProfileUrl(){
      this.setData({
        isShow:false 
      })

      this.setStorage('isShow',1)     
    },
    privacy_info(){
      this.router('/pages/cash-detail/index?type=privacy');
    },

 


     // 参数
  _requestPageList(isLoadMore = false, callback) {
    // isLoadMore 为true是上拉加载，不需要置空数组
    if(!isLoadMore){
      
      this.setData({
        goodsList:[], // 全部
        pageNo: 1
      });
    }
    let opt = {
      search:this.data.search,
      pageNo :this.data.pageNo 
    }
    var storageData = this.getStorage('user_info');
    if(storageData){
      opt['token']=storageData.wx_openid
    }

    // console.log('opt',opt)
    this._requestPageListCom("virtualTaskList", opt, callback);
  },
  // 列表接口
  _requestPageListCom(url, params, callback) {

    this.Index(params).then(res => {

      


      if (res.resCode == "9999") {
        
        let oldList = [];     
        oldList = this.data.goodsList;       
        const newGoodsList = res.data.like_list
        const allData = [...oldList, ...newGoodsList]     
        this.setData({
          goodsList:allData,
          recordCount: res.data.page,
        })
        if(callback && typeof callback === 'function'){
          callback()
        }
      }
    })
  },
  // 输入搜索 event.detail 为当前输入的值
  searchInp(e) {
    this.setData({
      searchVal: e.detail
    })
  },
  // 搜索
  searchClick(){
    this._requestPageList();
  },
  // tip: 如果在 bindchange 的事件回调函数中使用 setData 改变 current 值，则有可能导致 setData 被不停地调用，
  // 因而通常情况下请在改变 current 值前检测 source 字段来判断是否是由于用户触摸引起 event.detail = {current, source}
  // 滚动切换标签样式
  changeTab: function (e) {  
    this.setData({
      currentTab: e.detail.current
    });
    this._requestPageList();
  },
  // 点击标题切换当前页时改变样式
  changeNav: function (e) {
    let currentIndex = e.target.dataset.current;
    if (this.data.currentTaB == currentIndex) {
      return false;
    } else {
      this.setData({
        currentTab: currentIndex
      })
    }
  },
  
  // 详情
  goDetails:function(events){
    // console.log('ok')
    let storageId = events.currentTarget.dataset.posid;
    getApp().storageId=storageId
    wx.switchTab({
      url: '/pages/classify/index',
     //  success: function (e) {
     //   let page = getCurrentPages().pop();
     //   page.onShow();
     // }  
    })
  },


  
  // 上拉加载  滚动到底部/右边时触发
  allLoadMore(e){
    let _self = this;
    this._allLoadMore = true;
    this.setData({
      pageNo: ++_self.data.pageNo
    });
    this._requestPageList(true, () => {
      _self._allLoadMore = false;
    });
  },
  // 下拉刷新接口
  onAllRefresh() {
    if (this._allFreshing) return
    this._allFreshing = true
    this.setData({goodsList:[]})
    this._requestPageList(false, () => {
      // 当处理完数据刷新后，wx.stopPullDownRefresh可以停止当前页面的下拉刷新
      this.setData({
        triggered: false,
       
      })
      this._allFreshing = false
    });
  },
  pScrollTo(){
    // 返回顶部
    this.setData({
      scrollTop:0
    });
  },


  
  
})